/**
 * 远程获取新闻和新闻类别数据
 */
const fs = require('fs');
const axios = require('axios');

const APPCODE = '85988d4a464b4d2ebbcbca71fc2aace9';

let channels = null;
let news = null;

/**
 * 获取所有的新闻类别
 */
async function getNewsChannels() {
    if (channels === null) {
        let resp = await axios.get("http://ali-news.showapi.com/channelList", {
            headers: {
                Authorization: `APPCODE ${APPCODE}`
            },
        });
        channels = resp.data.showapi_res_body.channelList;
    } 
	return channels;
}

/**
 * 获取所有的新闻数据
 * @param {Number} channelId 频道id
 * @param {Number} page 页面，从1开始
 * @param {Number} limit 每页多少条数据
 */
async function getNews(channelId, page = 1, maxResult = 10) {
    if (news === null) {
        let resp = await axios.get("http://ali-news.showapi.com/newsList", {
            headers: {
                Authorization: `APPCODE ${APPCODE}`,
            },
            params: {
                channelId,
                page,
                maxResult,
                needAllList: false,
                needContent: 1,
            },
        });
        news = resp.data.showapi_res_body.pagebean;
    }
	return news;
}
/**
 * allNum 总新闻数
 * allPages 总页数
 * contentList 数组，新闻数据
 * currentPage 当前页码
 * maxResult 页容量
 */


